<?php

/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $first_name
 * @property string $last_name
 * @property string $right_value
 * @property integer $gender_id
 * @property integer $user_type_id
 * @property string $last_login
 * @property integer $status_id
 * @property integer $is_deleted
 *
 * The followings are the available model relations:
 * @property Accounts[] $accounts
 * @property Messages[] $messages
 * @property Messages[] $messages1
 * @property SavedSearches[] $savedSearches
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password', 'required'),
			array('gender_id, user_type_id, status_id, is_deleted', 'numerical', 'integerOnly'=>true),
			array('username, password, email, first_name, last_name', 'length', 'max'=>64),
			array('right_value', 'length', 'max'=>20),
			array('last_login', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, email, first_name, last_name, right_value, gender_id, user_type_id, last_login, status_id, is_deleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'accounts' => array(self::HAS_MANY, 'Accounts', 'user_id'),
			'messages' => array(self::HAS_MANY, 'Messages', 'from_user_id'),
			'messages1' => array(self::HAS_MANY, 'Messages', 'to_user_id'),
			'savedSearches' => array(self::HAS_MANY, 'SavedSearches', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'first_name' => 'First Name',
			'last_name' => 'Last Name',
			'right_value' => 'Right Value',
			'gender_id' => 'Gender',
			'user_type_id' => 'User Type',
			'last_login' => 'Last Login',
			'status_id' => 'Status',
			'is_deleted' => 'Is Deleted',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('right_value',$this->right_value,true);
		$criteria->compare('gender_id',$this->gender_id);
		$criteria->compare('user_type_id',$this->user_type_id);
		$criteria->compare('last_login',$this->last_login,true);
		$criteria->compare('status_id',$this->status_id);
		$criteria->compare('is_deleted',$this->is_deleted);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        protected function afterValidate()
	{   
		parent::afterValidate();
		$this->password = $this->encrypt($this->password);                     
	}
	
	public function encrypt($value)
	{
		return md5($value);
	}
}